home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / kernel.zip / K4.ASM < prev    next >
Assembly Source File  |  1986-11-25  |  14KB  |  499 lines

  1. ;Listing 4    High Resolution Clock
  2. ;(C) Copyright 1986 Ken Berry.
  3. ;All rights reserved.
  4. ;Copies may be made for non-commercial, private use only.
  5. ;
  6.  
  7.       include tele.mac ; system defintions (listing 2)
  8.  
  9.       extrn t_syxtm:word ; system execution time accumulator
  10.       extrn sys_dgrp:word ; data segment storage
  11.       extrn sys_stat:word ; original register block
  12.  
  13.       public t__tick ; system tick interrupt service
  14.       public t_astrm ; application task termination flag
  15.       public tmr_dspl ; physical display pointer
  16.       public tmr_dvsr ; timer period
  17.       public tmr_ilck ; tick service reentrant interlock
  18.       public tmr_sync ; synchronization function address
  19.       public tmr_tkct ; tick clock
  20.       public tmr_xtm ; tick service execution time
  21.       public tmr__clr ; reset time base generation
  22.       public tmr__int ; timer initialization function
  23.       public tmr__rst ; timer termination function
  24.       public tmr__sts ; read timer status
  25.       public tmr__tmr ; restart hardware timer
  26.       public t_rdclk ; read high resolution clock
  27.       public t_rtactg ; psuedo time accumulator pointer
  28.       public t_rtmark ; mark execution interval
  29.       public t__rdclk ; read real time clock
  30.       public td_ref ; clock update tick reference count
  31.       public td_tct ; clock tick timer
  32.       public td__set ; set time of day clock
  33.       public td__upd ; update time of day clock
  34.       public w__cdspl ; physical display update function
  35.       public w__sync ; physical display synchronization
  36.  
  37. RLCINT      equ 80h     ; relocated alternate time base interrupt
  38. TMRINT      equ 8      ; hardware timer interrupt
  39. TMRPRT      equ 40h     ; timer (8253) port
  40. TMRPRD      equ 19912     ; timer period (60 Hz rate)
  41. ;TMRPRD    equ 9956      ; timer period (120 Hz rate)
  42. INTPRT      equ 20h     ; interrupt controller (8259) port
  43. TMRMSK      equ 01h     ; hardware timer interrupt mask
  44. INTEOI      equ 20h     ; interrupt termination value
  45. DSPCT      equ 1      ; 60 Hz interrupt rate
  46. ;DSPCT       equ 2      ; 120 Hz interrupt rate
  47. IDV0      equ 3      ; tmr_idv0 divisor
  48. ISKP0      equ 776     ; tmr_ict correction value
  49. ISKP1      equ 11     ; tmr_idv1 correction value
  50. ISKP2      equ 38     ; tmr_idv2 correction value
  51.  
  52.       dseg
  53.  
  54. tmr_tkct  dw 0         ; interrupt counter
  55. tmr_dct   db 0         ; display counter
  56. tmr_ict   dw 0         ; tick clock (for time base generation)
  57. tmr_dvsr  dw TMRPRD     ; 1/2 timer period
  58. t_astrm   db 0FFh     ; application task termination flag
  59. tmrflg      db 0FFh     ; system state flag (t__tick)
  60. tmr_ilck  db 0         ; tick service reentrant interlock
  61. tmr_idv0  db 0         ; clock time base generator
  62. tmr_idv1  db 0         ; primary alternate time base generator
  63. tmr_idv2  db 0         ; secondary alternate time base generator
  64. tmr_dspl  dw 0         ; console display w_pwdw pointer
  65. t_rtactg  dw 0         ; psuedo time accumulator pointer
  66. t_rtrfct  dw 0         ; real time reference count
  67. t_rttick  dw 0         ; tick clock phase
  68. tmr_xtm   dw 3 dup (0)     ; tick service psuedo time accumulator
  69. tmrpxtm   dw 0         ; prior psuedo time accumulator pointer
  70. tmr_sync  dw offset pgroup:w__sync ; synchronization function pointer
  71. td_ref      dw 0         ; clock update tick reference count
  72. td_tct      dw 0         ; clock tick timer
  73.  
  74.       endds
  75.  
  76.       pseg
  77.  
  78. ;t__tick          system tick service
  79. ;
  80. ;t__tick\\
  81. ;
  82. ;Control only comes here in response to an interrupt from the system clock.
  83. ;This function serves three purposes. It maintains the system clock, which
  84. ;provides the current date and time for both system and application uses. It
  85. ;also  performs an update of the first physical display. And finally it
  86. ;terminates the execution interval for the current application task.
  87. ;
  88.  
  89. t__tick   proc far
  90.  
  91. ; reentrant lockout
  92.  
  93.       assume ss:nothing,ds:nothing,es:nothing
  94.       sti         ; interrupts on
  95.       push ds     ; protect ds
  96.       push ax     ; protect ax
  97.       mov ax,dgroup  ; establish data addressability
  98.       mov ds,ax
  99.       assume ds:dgroup
  100.       mov al,INTEOI  ; terminate interrupt
  101.       out INTPRT,al
  102.       ilck al,tmr_ilck ; test for not reentrant call
  103.       or al,al
  104.       jz tick
  105.       pop ax     ; restore ax
  106.       pop ds     ; restore ds
  107.       iret         ; return from interrupt
  108.  
  109. ; system interlock
  110.  
  111. tick:      mov t_astrm,0FFh ; terminate application task
  112.       sys_entr tmrflg ; enter system state
  113.  
  114. ; set machine environment
  115.  
  116.       sys_sctx     ; save processor context
  117.       push bp     ; protect bp
  118.       mov bp,sp     ; mark stack location
  119.       lea ax,tmr_xtm ; accumulate psuedo time
  120.       push ax
  121.       call t_rtmark
  122.       mov sp,bp
  123.       mov tmrpxtm,ax ; store prior pointer
  124.  
  125. ; real time system processing
  126.  
  127.       inc tmr_dct     ; remove display harmonics
  128.       mov al,DSPCT
  129.       xor al,tmr_dct
  130.       jnz tick4
  131.       mov tmr_dct,al
  132.       push tmr_dspl  ; display physical window
  133.       call w__cdspl
  134.       mov sp,bp     ; restore stack pointer
  135.       inc tmr_ict     ; increment interrupt counter
  136.       inc tmr_tkct     ; increment tick clock
  137.  
  138. ; time base generation
  139.  
  140.       mov ax,ISKP0     ; long term time base correction
  141.       xor ax,tmr_ict
  142.       jnz tick1
  143.       mov tmr_ict,ax
  144.       call tick5     ; update system tick clock
  145. tick1:      inc tmr_idv0     ; generate clock time base
  146.       mov al,IDV0
  147.       xor al,tmr_idv0
  148.       jnz tick3
  149.       mov tmr_idv0,al
  150.       call tick5     ; update system tick clock
  151.       inc tmr_idv1     ; primary alternate time base correction
  152.       mov al,ISKP1
  153.       xor al,tmr_idv1
  154.       jnz tick2
  155.       mov tmr_idv1,al
  156.       int RLCINT     ; update alternate time base
  157.       inc tmr_idv2     ; secondary alternate time base correction
  158.       mov al,ISKP2
  159.       xor al,tmr_idv2
  160.       jnz tick2
  161.       mov tmr_idv2,al
  162.       int RLCINT     ; update alternate time base
  163. tick2:      int RLCINT     ; update alternate time base
  164.  
  165. ; terminate interrupt service
  166.  
  167. tick3:      push tmrpxtm     ; restore original psuedo time accumulator
  168.       call t_rtmark
  169.       mov sp,bp
  170.       pop bp     ; restore bp
  171.       test tmrflg,0FFh ; test for interrupted system task
  172.       jnz tick4
  173.       xor ax,ax     ; terminate task
  174.       mov tmr_ilck,al ; enable reentrance
  175.       retn         ; near return to system task management
  176.  
  177. tick4:      sys__rctx     ; restore processor context
  178.       cli         ; interrupts off
  179.       mov tmr_ilck,0 ; enable reentrance
  180.       pop ds     ; restore ds
  181.       iret         ; return to interrupted task
  182.  
  183. ; update system tick counter
  184.  
  185. tick5:      mov ax,td_tct  ; test for no overflow
  186.       inc ax
  187.       cmp ax,td_ref
  188.       jne tick6
  189.       call td__upd     ; update clock
  190.       xor ax,ax     ; reset tick counter
  191.       mov td_ref,ax
  192.       mov td_tct,ax
  193. tick6:      inc td_tct     ; increment tick counter
  194.       retn         ; return
  195.  
  196. t__tick   endp
  197.  
  198. ;tmr__int          initialize timer
  199. ;
  200. ;tmr__int()
  201. ;
  202. ;All data areas necessary for clock maintenance are initialized. The hardware
  203. ;timer is programmed for the appropriate rate and its interrupt vector is made
  204. ;to point to sys_tmr. The original vector is relocated and will be used by
  205. ;sys_tmr as the alternate time base.
  206. ;
  207.  
  208. tmr__int  proc near
  209.       call tmr__dsi  ; diable interrupts
  210.       mov ax,dgroup  ; set data segment
  211.       mov sys_dgrp,ax
  212.       mov ax,cs     ; set code segment
  213.       lea si,sys_stat
  214.       mov [si].rcs,ax
  215.       cli         ; interrupts off
  216.       mov tmr_ilck,0FFh ; lockout t__tick
  217.       mov bx,tmr_sync ; test for no synchronization function
  218.       test bx,bx
  219.       jz int0
  220.       lea bx,tmr_sync ; synchronize timer interrupt
  221.       call [bx]
  222.       jmp short int1 ; continue
  223. int0:      call tmr__tmr  ; start timer
  224. int1:      call t_rdclk     ; set real time clock phase
  225.       mov t_rttick,ax
  226.       mov t_rtrfct,ax ; set reference count
  227.       mov t_rtactg,offset dgroup:t_syxtm ; initialize time accumulator
  228.       call td__set     ; set current time
  229.       sti         ; interrupts on
  230.       xor ax,ax     ; form 0
  231.       push ds     ; protect ds
  232.       mov ds,ax     ; relocate original interrupt vector
  233.       mov di,ax
  234.       cli
  235.       mov ax,[di+4*TMRINT]
  236.       mov [di+4*RLCINT],ax
  237.       mov ax,[di+4*TMRINT+2]
  238.       mov [di+4*RLCINT+2],ax
  239.       mov ax,offset pgroup:t__tick ; set interrupt service
  240.       mov [di+4*TMRINT],ax
  241.       mov ax,cs
  242.       mov [di+4*TMRINT+2],ax
  243.       sti         ; interrupts on
  244.       pop ds     ; restore ds
  245.       call tmr__eni  ; enable interrupts
  246.       ret         ; return
  247. tmr__int  endp
  248.  
  249. ;
  250. ;tmr__clr          reset time base generation
  251. ;
  252. ;tmr__clr()
  253. ;
  254. ;The time base adjustment variables are reset. This function is to be called by
  255. ;td__set when the time of day is initially set from a continuous clock. Nothing
  256. ;is returned.
  257. ;
  258.  
  259. tmr__clr  proc near
  260.       xor ax,ax     ; zero time base generation variables
  261.       mov tmr_id